package components.navigation.degree.renderers
{
	import components.navigation.degree.DegreeTab;
	
	import controls.DataHub;
	
	import data.ClassGroup;
	import data.ClassSection;
	import data.Course;
	import data.User;
	
	import org.un.cava.birdeye.qavis.treemap.controls.treeMapClasses.TreeMapBranchHeader;

	public class ColoredBranchHeader extends TreeMapBranchHeader
	{
		public function ColoredBranchHeader()
		{
			super();
		}
		
		private var hub:DataHub = DataHub.instance;
		
		private var _data:Object;
		
		override public function get data():Object
		{
			return _data;
		}
		
		override public function set data(value:Object):void
		{
			//super.data = value;
			if(!(value is ColoredBranchRenderer))
				trace("not renderer");
			
			_data = value;
			if(!isNaN(_data.data.gpa))
			{
				var color:uint = getColor();
				this.selectionButton.setStyle('fillColors', [color, color]);
			}
			this.invalidateProperties();
			this.invalidateSize();
			this.invalidateDisplayList();
		}		
		
		private function getColor():uint
		{
			if(_data.data.hasOwnProperty('course'))
				return getCourseColor();
			else if(_data.data.hasOwnProperty('group'))
				return getGroupColor();
			return colorByGPA(_data.data.gpa);
		}
	
		private function getGroupColor():uint
		{
			var user:User = hub.user;
			var group:ClassGroup = _data.data.group as ClassGroup;
			
			if(user.fulfillsGroupRequirement(group))
				return DegreeTab.TAKEN;
			return colorByGPA(group.gpa);
		}
		
		private function getCourseColor():uint
		{
			var user:User = hub.user;
			var course:Course = _data.data.course;
			
			if(user.hasTakenCourse(course))
				return DegreeTab.TAKEN;
			if(!user.canTake(course))
				return DegreeTab.REQUIREMENTS_NOT_MET;
			if(!offeredThisSemester(course))
				return DegreeTab.NOT_OFFERED;
			return colorByGPA(course.gpa);
		}
		
		private function colorByGPA(gpa:Number):uint
		{
			if(gpa > 3.5)
				return DegreeTab.GOOD_GPA;
			if(gpa > 3.0)
				return DegreeTab.B_OR_BETTER_GPA;
			if(gpa > 2.5)
				return DegreeTab.C_PLUS_GPA;
			if(gpa > 2.0)
				return DegreeTab.C_GPA;
			return DegreeTab.NOT_OFFERED;
		}
		
		private function offeredThisSemester(course:Course):Boolean
		{
			for each(var section:ClassSection in course.sections)
			{
				if(section.semester == hub.currentSemester)
					return true;
			}
			return false;
		}
	}
}